package com.weixing.mall.provider.config;

import com.weixing.mall.core.config.SwaggerConfiguration;
import com.weixing.mall.core.config.WeiXingCoreConfig;
import com.weixing.mall.core.converter.EnumConvertFactory;
import com.weixing.mall.core.converter.MyApiParamParameterBuilder;
import com.weixing.mall.core.converter.MySwaggerExpandedParameterBuilder;
import com.weixing.mall.core.interceptor.AuthInterceptor;
import com.weixing.mall.core.support.auth.AuthInfoArgsResolver;
import com.weixing.mall.provider.interceptor.LogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/7/24 15:01
 * @Modified By:
 */
@Configuration
@Import({SwaggerConfiguration.class,WeiXingCoreConfig.class, MyApiParamParameterBuilder.class, MySwaggerExpandedParameterBuilder.class})
public class DepotWebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private  EnumConvertFactory enumConvertFactory;
    @Autowired
    private AuthInterceptor authInterceptor;
    @Autowired
    private AuthInfoArgsResolver authInfoArgsResolver;
    /**
     * 19      * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。
     * 20      * 需要重新指定静态资源
     * 21      * @param registry
     * 22
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        super.addResourceHandlers(registry);
    }

    /**
     * Description:  <br/>
     *
     * @param:
     * @return:
     * @Author: kfc
     * @Date: 2018/4/4 16:23
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathPatterns = Arrays.asList("/webjars/**", "/swagger-ui.html", "/static/**", "/druid/**", "/captcha","/api","/oauth","/error");
        //拦截规则：除了login，其他都拦截判断
        registry.addInterceptor(authInterceptor);
        registry.addInterceptor(logInterceptor()).addPathPatterns("/**").excludePathPatterns(excludePathPatterns);
        super.addInterceptors(registry);

    }

    /**
     * 日志拦截器
     *
     * @return
     */
    @Bean
    public LogInterceptor logInterceptor(){
        return new LogInterceptor();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        //注入的参数解析器
        argumentResolvers.add(authInfoArgsResolver);
    }




    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(enumConvertFactory);
    }
}
